<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function _initialize(){
        if(!session('UserInfo')){
            header('location:'.U('Login/login'));exit;
        }
    }
    public function index(){
	
    	$this->display();
    }




    //相册管理页
    public function album_controller(){
    	$cls_album = new AlbumModelController();
    	$data = $cls_album->get_album_all();
    	$this->assign('data',$data);
        $this->assign('DataListCount',count($data['list']));
    	//dump($data);
    	$this->display();
    }


    //单相册管理
    public function album_controller_edit(){
        $cls_imgs = new ImgsModelController();
        session('album_id',I('get.album_id'));
        $where['imgs_aid'] = session('album_id');
        $data = $cls_imgs->get_imgs('',$where);
        $this->assign('data',$data);
        $this->assign('album_id',I('get.album_id'));
        $this->assign('DataListCount',count($data['list']));
        $this->display();
    }  


        public function album_controller_edit_upload(){
            if(count($_FILES['img']['name']) > 9){
                    echo "<script>alert('最多同时上传9张图片！');location.href='album_controller_edit?album_id={$aid}';</script>";exit;
            }
            $aid = I('post.album_id');
         //   echo "<script>alert('{$aid}');</script>";
            if(!$aid){
                echo "<script>alert('缺失相册id');</script>";exit;
            }
            $Types = array('image/gif','image/jpeg','image/png');
            $Exts = array('jpg','jpeg','png','gif','JPG','JPEG','PNG','GIF');
            $Size = 700000;
            $Err_Key = array();
            foreach($_FILES['img'][size] as $k=>$v){
                if($v > $Size){
                    $Err_Key[] = $k;
                }
            }
        
            foreach($_FILES['img']['name'] as $k=>$v){
                if(in_array($k,$Err_Key)){
                    continue;
                }
                $PExt = pathinfo($v,PATHINFO_EXTENSION);
                if(!in_array($PExt,$Exts)){
                    $Err_Key[] = $k;
                }
            }

            foreach($_FILES['img']['type'] as $k=>$v){
                if(in_array($k,$Err_Key)){
                    continue;
                }
                if(!in_array($v,$Types)){
                    $Err_Key[] = $k;
                }
            }


            $m = M('Imgs');
            foreach($_FILES['img']['tmp_name'] as $k=>$v){
                $CExt = pathinfo($_FILES['img']['name'][$k],PATHINFO_EXTENSION);
                if(in_array($k,$Err_Key)){
                    continue;
                }  
                if(is_uploaded_file($v)){
                    $Dir = 'Public/images/album/';
                    if(!is_dir($Dir.$aid)){
                        mkdir($Dir.$aid);
                    }
                    $File_Name = $aid.'/'.md5(microtime().rand(111111111,999999999)).'.'.$CExt;
                    if(move_uploaded_file($v,$Dir.$File_Name)){
                        $data['imgs_aid'] = $aid;
                        $data['imgs_src'] = $File_Name;
                        $data['imgs_ctime'] = time();
                        if($m->create($data)){
                            $st = $m->add();
                            if($st > 0){
                                $st = 'ok';
                            }
                        }
                    }else{
                        echo 1;exit;
                    }
                }else{
                    echo "<script>alert('操作非法！');</script>";exit;
                }
            }
            if($st == 'ok'){
                if(count($Err_Key)>0){
                    echo "<script>alert('添加成功，但部分图片超限，未上传！');location.href='album_controller_edit?album_id={$aid}';</script>";exit;
                }else{
                    echo "<script>alert('添加成功！');location.href='album_controller_edit?album_id={$aid}';</script>";exit;
                }
            }else{
                echo "<script>alert('添加失败！');location.href='album_controller_edit?album_id={$aid}';</script>";exit;
            }
        }


    //篾片管理
    public function pic_controller(){
        $cls_imgs = new ImgsModelController();
        $data = $cls_imgs->get_imgs();
        $this->assign('data',$data);
        $this->assign('DataListCount',count($data['list']));
        $this->display();
    }




    //用户管理
    public function user_controller(){
        $cls_User = new UsersModelController();
 
        $data_Res = $cls_User->get_user_list();

        $this->assign('data',$data_Res);
        $this->assign('DataListCount',count($data_Res['list']));
        $this->display();
    }

    //用户登陆信息列表页
    public function user_controller_detail(){
        if(I('get.oid')){
            session('openid',I('get.oid'));
        }
        if(I('get.name')){
            session('users_name',I('get.name'));
        }
        $this->assign('name',session('users_name'));
        
        $m = M('Users_login');
        $where['oid'] = session('openid');
        $data_UserLogin = Get_Fpage($m,$fields='',$where,$order='id desc',$pcount=15);
        $this->assign('data',$data_UserLogin);
        $this->display();
    }

    public function settings(){
        $this->display();
    }

    public function settings_sub(){
        $old_pwd = I('post.old_pwd');
        $new_pwd = I('post.new_pwd');
        $new_pwd2 = I('post.new_pwd2');
        if(!$old_pwd || !$new_pwd || !new_pwd2){
            echo "<script>alert('信息不完整！');history.go(-1);</script>";exit;
        }
        if($new_pwd != $new_pwd2){
            echo "<script>alert('新密码不一致！');history.go(-1);</script>";exit;
        }
        $UserInfo = session('UserInfo');

        $m = M('Admins');
        $res_Admins = $m->where(array('id'=>$UserInfo['id']))->find();
        if($res_Admins['pwd'] == md5($old_pwd)){
            if(md5($new_pwd) == $res_Admins['pwd']){
                echo "<script>alert('新旧密码相同！');history.go(-1);</script>";exit;
            }
            $st = $m->where(array('id'=>$UserInfo['id']))->data(array('pwd'=>md5($new_pwd)))->save();
            if($st){
                echo "<script>alert('密码修改成功！');history.go(-1);</script>";exit;
            }else{
                echo 'err';
            }
        }else{
            echo "<script>alert('原密码不正确！');history.go(-1);</script>";exit;
        }

    }

}